﻿using eProcurement.BLL;
using eProcurement.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement
{
    public partial class Signup2 : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {

        }

        protected void btnRegister_Click( object sender, EventArgs e )
        {
            Manager manager = new Manager();
            lblPasswordMatchError.Visible = false;
            erruser.Visible = true;
            bool isformgood = true;
            errpwdlength.Visible = false;

            if ( manager.UserExist( txtUsername.Value.Trim() ) )
            {
                isformgood = false;
                erruser.Visible = true;
            }

            if ( txtPassword.Value.Trim() == string.Empty || txtCPassword.Value.Trim() == string.Empty || txtPassword.Value != txtCPassword.Value )
            {
                isformgood = false;
                lblPasswordMatchError.Visible = true;
            }

            if ( txtPassword.Value.Count() < 6 )
            {
                isformgood = false;
                errpwdlength.Visible = true;
            }

            if ( !chkAgree.Checked )
            {
                isformgood = false;
            }

            if ( isformgood )
            {
                Purchaser appuser = new Purchaser();

                Guid id = Guid.NewGuid();

                appuser.Id = id;
                appuser.PurchaserId = id;
                appuser.DateJoined = DateTime.Now;

                //Profile Details
                appuser.CompanyName = txtCompanyName.Value;
                appuser.Website = txtWebsite.Value;
                appuser.Address1 = txtAddress1.Value;
                appuser.Address2 = txtAddress2.Value;
                appuser.City = txtCity.Value;
                appuser.Description = txtDescription.Value;

                //Personal Details
                appuser.FirstName = txtFirstname.Value;
                appuser.LastName = txtLastname.Value;
                appuser.JobTitle = txtJobTitle.Value;
                appuser.Telephone = txtTelephone.Value;
                appuser.Fax = txtFax.Value;

                //Account Details
                appuser.Username = txtUsername.Value.Trim();
                appuser.Password = txtPassword.Value;

                //payment details
                appuser.pFirstname = txtpFirstName.Value;
                appuser.pLastName = txtpLastName.Value;
                appuser.CC = txtCC.Value;
                appuser.ExpMonth = txtMonth.Value;
                appuser.ExpYear = txtYear.Value;
                appuser.Code = txtCode.Value;
                appuser.pAddress1 = txtpAddress1.Value;
                appuser.pAddress2 = txtpAddress2.Value;
                appuser.pCity = txtpCity.Value;

                if ( manager.AddPurchaser( appuser ) )
                {
                    FormsAuthentication.RedirectFromLoginPage( txtUsername.Value, false );
                }
            }
        }
    }
}